const { formatResponse } = require('./tool')

// 自定义错误
class ServiceError extends Error {
    constructor(message, code) {
        super(message)
        this.code = code
    }
    toResponseJson() {
        return formatResponse(null, this.message, this.code)
    }
}

// 文件上传错误
exports.UploadError = class extends ServiceError {
    constructor(message) {
        super(message, 413)
    }
}

// 禁止访问错误
exports.ForbiddenError = class extends ServiceError {
    constructor(message) {
        super(message, 401)
    }
}

// 验证错误 
exports.ValidationError = class extends ServiceError {
    constructor(message) {
        super(message, 406)
    }
}

// 无资源错误
exports.NotFoundError = class extends ServiceError {
    constructor() {
        super('not find', 404)
    }
}

// 未知错误
exports.UnKnowError = class extends ServiceError {
    constructor() {
        super('server internal error', 500)
    }
}

module.exports.ServiceError = ServiceError